package com.wjr.braveheart.adapter.listView;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.SpannableString;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

/**
 * Created by weijinran ，Email 994425089@qq.com，on 2017/10/26.
 * Describe: ListView通用Adapter的ViewHolder
 * PS: Not easy to write code, please indicate.
 */

public class ViewHolder {
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    public ViewHolder(Context context, ViewGroup parent, int layoutId,
                      int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        mConvertView.setTag(this);
    }

    public static ViewHolder get(Context context, View convertView, ViewGroup parent,
                                 int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        } else {
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.mPosition = position;
            return viewHolder;
        }
    }

    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public int getPosition() {
        return mPosition;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为TextView设置SpannableString字符串
     *
     * @param viewId
     * @param spannableString
     * @return
     */
    public ViewHolder setText(int viewId, SpannableString spannableString) {
        TextView view = getView(viewId);
        view.setText(spannableString);
        return this;
    }

    /**
     * 为TextView设置背景颜色
     *
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setTextBackgroundColor(Context context, int viewId, int resId) {
        TextView view = getView(viewId);
        view.setBackgroundColor(ContextCompat.getColor(context, resId));
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bitmap
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param path
     * @return
     */
    public ViewHolder setImagePath(int viewId, String path, HolderImageLoader imageLoader) {
        //使用第三方网络加载框架进行封装
        ImageView imageView = getView(viewId);
        imageLoader.loadImage(imageView, path);
        return this;
    }


    /**
     * 图片的加载接口
     */
    public interface HolderImageLoader {

        /**
         * 需要去复写这个方法来加载图片
         *
         * @param imageView
         * @param path
         */
        void loadImage(ImageView imageView, String path);

    }
}
